import java.util.HashSet;
import java.util.Iterator;
import java.lang.Math;

public class DisplayableAutomaton extends Automaton {
	protected HashSet<DisplayableState>	displayedStates;
	int spaceBetweenStates;
	int spaceBetweenStatesAndBorder;
	
	public DisplayableAutomaton(Automaton autom, int spBtSt, int spBtStAB) 
	{
		spaceBetweenStates = spBtSt;
		spaceBetweenStatesAndBorder = spBtStAB;
		displayedStates = new HashSet<DisplayableState>();
		
		Iterator<State> stateI = autom.states.iterator();
		//calculus of number of state by line
		Double lengthAutom = new Double(autom.states.size());
		Double sqrtLengthAutom = Math.sqrt(lengthAutom);
		sqrtLengthAutom = Math.ceil(sqrtLengthAutom);
		int statesByLine = sqrtLengthAutom.intValue();
		int x,y,i;
		State s;
		
		
		
		x = spaceBetweenStatesAndBorder;
		y = spaceBetweenStatesAndBorder;
		i = 0;
		while(stateI.hasNext())
		{
			s = stateI.next();
			
			//System.out.println("("+x+","+y+")");
			displayedStates.add(new DisplayableState(s,x,y));
			
			i++;
			x += spaceBetweenStates;
			if(i%statesByLine == 0)
			{
				x = spaceBetweenStatesAndBorder;
				y+= spaceBetweenStates;
			}
		}
		
		//transition update
		
		Iterator<DisplayableState> dStateI = this.displayedStates.iterator();
		DisplayableState dsTemp,dsReasTemp;
		Transition tranTemp;
		while(dStateI.hasNext())
		{
			dsTemp = dStateI.next();System.out.println("dStateI");
			
			Iterator<Transition> dTransI = dsTemp.getTransitions().iterator();
			while(dTransI.hasNext())
			{
				tranTemp = dTransI.next();System.out.println("dTransI");
				Iterator<DisplayableState> dStateIReasearch = this.displayedStates.iterator();
				while (dStateIReasearch.hasNext())
				{
					dsReasTemp = dStateIReasearch.next();System.out.println("dsReasTemp");
					if (dsReasTemp.getOriginalState().getStateId() == tranTemp.getTargetState().getStateId())
					{
						tranTemp.setTargetState(dsReasTemp);
					}
				}
			}
		}
	}
	
	public String toString()
	{
		return name + "\n" + displayedStates;
	}

	public HashSet<DisplayableState> getDisplayedStates() {
		return displayedStates;
	}

	public int getSpaceBetweenStates() {
		return spaceBetweenStates;
	}
}
